/**
 * 返回将表单数据序列化为 k=v&k=v
 * @param {Object} form 表单对象
 */
function serializeKeyValue(form) {
  var params = [];
  for (var i = 0; i < form.elements.length; i++) {
    var field = form.elements[i];
    switch (field.type) {
      case "file":
      case "submit":
      case "reset":
      case "button":
        break;
      case "radio":
      case "checkbox":
        if (!field.checked) {
          break;
        }
      default:
        //不包含没有名字的表单字段
        if (field.name.length) {
          params.push(
            encodeURIComponent(field.name) +
              "=" +
              encodeURIComponent(field.value)
          );
        }
    }
  }
  return params.join("&");
}
/**
 * 序列化为JS对象{'k':v,'k':v}
 * @param {Object} form 当前表单对象
 */
function serializeJSObject(form) {
  var arr = {};
  for (var i = 0; i < form.elements.length; i++) {
    var field = form.elements[i];
    switch (field.type) {
      case undefined:
      case "button":
      case "file":
      case "reset":
      case "submit":
        break;
      case "checkbox":
        if (field.checked) {
          if (arr[field.name]) {
            arr[field.name].push(field.value);
          } else {
            // arr[field.name] = new Array();
            arr[field.name] = [field.value];
          }
        }
        break;
      case "radio":
        if (!field.checked) {
          break;
        }
      default:
        arr[field.name] = field.value;
    }
  }
  return arr;
}

function MyGetDate(url,name) {
  return new Promise((resolve, reject)=>{
    var xhr=new XMLHttpRequest();	
    xhr.onreadystatechange=function(){				
      if(xhr.readyState==4){
        if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
          return resolve(xhr.responseText);//成功
        } else {
          return reject(xhr.responseText);//失败
        }
      }
    }

    if (name) {
      url+="?name="+name
    }

    xhr.open("GET",url)
    xhr.send();
  })
}

function MyPostDate(url,data) {	
  return new Promise(function(resolve,reject){
    var xhr=new XMLHttpRequest();				
    xhr.onreadystatechange=function(){									
      if(xhr.readyState==4){						
        if(xhr.status>=200&&xhr.status<=300||xhr.status==304){
          return resolve(xhr.responseText);//成功
        }else{
          return reject(xhr.responseText);//失败
        }
      }
    }				
    xhr.open("POST",url)	
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")			
    xhr.send(data); 
    window.location.href="index.html";
  })
}

function select(url,name) {
  let a=MyGetDate(url,name);	
  a.then((value)=>{
    var arr=JSON.parse(value)
    var str=``;
    arr.forEach((stu) => {
      str+=`
        <tr>
          <td>${stu.id}</td>
          <td>${stu.clazz}</td>
          <td>${stu.name}</td>					 
          <td>${stu.gender}</td>
          <td>${stu.age}</td>
          <td>${stu.hobby}</td>
          <td>${stu.tel}</td>
          <td>${stu.address}</td>
          <td>${stu.remark}</td>
          <td>${stu.date}</td>
          <td><a href="update.html#${stu.id}">修改</a></td>
          <td><a href="javascript:del(${stu.id});">删除</a></td>
        </tr>
        `
    });
    document.querySelector('tbody').innerHTML=str;
  }).catch(err=>{
    console.log(err);
  })
}

function huixian(url){
  let aa=MyGetDate(url);
  aa.then((value)=>{
          var arr=JSON.parse(value)      
          document.querySelector("#id").value = arr[0].id
          document.querySelector("#clazz").value=arr[0].clazz
          document.querySelector("#name").value=arr[0].name
          document.querySelector('input[value="' + arr[0].gender + '"]').checked = true
          document.querySelector("#age").value = arr[0].age
          if (arr[0].hobby.includes('吃饭')) {
            document.querySelectorAll("input[name='hobby']")[0].checked = true
          }
          if (arr[0].hobby.includes('睡觉')) {
            document.querySelectorAll("input[name='hobby']")[1].checked = true
          }
          if (arr[0].hobby.includes('学习')) {
            document.querySelectorAll("input[name='hobby']")[2].checked = true
          }
          document.querySelector("#tel").value = arr[0].tel
          document.querySelector('option[value="' + arr[0].address + '"]').selected = true
          document.querySelector("#remark").value = arr[0].remark
          document.querySelector("#date").value = arr[0].date
        }).catch(err=>{
          console.log(err);
        })
}


